/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.jy.springbootcmsservice.modules.sys.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.jy.springbootcmsservice.common.base.entity.DataEntity;
import com.jy.springbootcmsservice.common.config.Global;
import com.jy.springbootcmsservice.common.constant.Punctuation;
import com.jy.springbootcmsservice.common.utils.CollectionsUtils;
import lombok.Data;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 用户Entity
 * @author tm
 * @version 2020-11-23
 */
@Data
public class User extends DataEntity<User> {

	/**
	 * 归属公司
	 */
	private Office company;
	/**
	 * 归属部门
	 */
	private Office office;
	/**
	 * 登录名
	 */
	private String loginName;
	/**
	 * 密码
	 */
	private String password;
	/**
	 * 工号
	 */
	private String no;
	/**
	 * 姓名
	 */
	private String name;
	/**
	 * 邮箱
	 */
	private String email;
	/**
	 * 电话
	 */
	private String phone;
	/**
	 * 手机
	 */
	private String mobile;
	/**
	 * 用户类型
	 */
	private String userType;
	/**
	 * 最后登陆IP
	 */
	private String loginIp;
	/**
	 * 最后登陆日期
	 */
	private Date loginDate;
	/**
	 * 是否允许登陆
	 */
	private String loginFlag;
	/**
	 * 头像
	 */
	private String photo;

	/**
	 * 原登录名
	 */
	private String oldLoginName;
	/**
	 * 新密码
	 */
	private String newPassword;
	/**
	 * 上次登陆IP
	 */
	private String oldLoginIp;
	/**
	 * 上次登陆日期
	 */
	private Date oldLoginDate;
	/**
	 * 根据角色查询用户条件
	 */
	private Role role;
	/**
	 * 拥有角色列表
	 */
	private List<Role> roleList = new ArrayList<>();

	public User() {
		super();
		this.loginFlag = Global.YES;
	}
	
	public User(String id){
		super(id);
	}

	public User(String id, String loginName){
		super(id);
		this.loginName = loginName;
	}

	public User(Role role){
		super();
		this.role = role;
	}

	@JsonIgnore
	public List<String> getRoleIdList() {
		List<String> roleIdList = new ArrayList<>();
		for (Role role : roleList) {
			roleIdList.add(role.getId());
		}
		return roleIdList;
	}

	public void setRoleIdList(List<String> roleIdList) {
		roleList = new ArrayList<>();
		for (String roleId : roleIdList) {
			Role role = new Role();
			role.setId(roleId);
			roleList.add(role);
		}
	}

	/**
	 * 用户拥有的角色名称字符串, 多个角色名称用','分隔.
	 */
	public String getRoleNames() {
		return CollectionsUtils.extractToString(roleList, "name", Punctuation.COMMA);
	}
	
	public boolean isAdmin(){
		return isAdmin(this.id);
	}
	
	public static boolean isAdmin(String id){
		return id != null && "1".equals(id);
	}
	
	@Override
	public String toString() {
		return id;
	}
}